local dengji = fk.CreateSkill {
  name = "ls__dengji",
  tags = { Skill.Limited }
}

local U = require "packages/utility/utility"
local ls = require "packages/lvshi/utility"

Fk:loadTranslationTable{
  ["ls__dengji"] = "登极",
  ["@ls__dengji"] = "登极",
  [":ls__dengji"] = "限定技，若你的手牌数不为唯一最大，你可视为使用一张基本牌；"..
  "<br />若“登极”行置，当此名称的牌进入弃牌堆后，你摸一张牌；"..
  "<br />当你的手牌数变为唯一最大后，你重置“登极”。",
  ["#ls__dengji"] = "你可视为使用一张基本牌",

  ["$ls__dengji1"] = "（杀）天威浩荡，赞颂吾名！",
  ["$ls__dengji2"] = "（闪）现在走，是你最好的选择。",
  ["$ls__dengji3"] = "（桃，酒，梨，卷）勤政为民，恩泽天下。",
  ["$ls__dengji4"] = "（毒）逆我者，不可留。",
}

dengji:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@ls__dengji", 0)
end)

dengji:addEffect("viewas", {
  mute = true,
  pattern = ".|.|.|.|.|basic",
  prompt = "#ls__dengji",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(dengji.name, all_names, nil, {})
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = dengji.name
    return card
  end,
  before_use = function (self, player, use)
    local i = 1
    if use.card.trueName == "jink" then i = 2
    elseif table.contains({"peach", "analeptic", "pear", "scroll"}, use.card.trueName) then i = 3
    elseif use.card.trueName == "poison" then i = 4
    end 
    player.room:notifySkillInvoked(player, dengji.name)
    player:broadcastSkillInvoke(dengji.name, i)
  end,
  after_use = function (self, player, use)
    player.room:setPlayerMark(player, "@ls__dengji", use.card.trueName)
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(dengji.name, Player.HistoryGame) == 0
    and table.find(Fk:currentRoom().alive_players, function (p)
      return p:getHandcardNum() >= player:getHandcardNum() and p ~= player
    end)
  end,
  enabled_at_response = function(self, player, response)
    if response then return false end
    return player:usedSkillTimes(dengji.name, Player.HistoryGame) == 0
    and table.find(Fk:currentRoom().alive_players, function (p)
      return p:getHandcardNum() >= player:getHandcardNum() and p ~= player
    end)
  end,
})

dengji:addEffect(fk.AfterCardsMove, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(dengji.name) and player:getMark("@ls__dengji") ~= 0 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == player:getMark("@ls__dengji") then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, dengji.name)
    player.room:notifySkillInvoked(player, dengji.name, "drawcard")
  end,
})

dengji:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(dengji.name) and player:getMark("@ls__dengji") ~= 0 then
      for _, move in ipairs(data) do
        if (move.from and move.from ~= player) or (move.to and move.to == player) then
          return table.every(player.room:getOtherPlayers(player), function (p)
            return p:getHandcardNum() < player:getHandcardNum() end)
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@ls__dengji", 0)
    player:setSkillUseHistory(dengji.name, 0, Player.HistoryGame)
  end,
})


return dengji